package com.demo.springbooot_demo.controller;

import com.demo.springbooot_demo.pojo.ResponseMessage;
import com.demo.springbooot_demo.pojo.User;
import com.demo.springbooot_demo.pojo.dto.LoginUserDto;
import com.demo.springbooot_demo.pojo.dto.UserDto;
import com.demo.springbooot_demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController //接口方法返回对象转换成json文本
@RequestMapping("/user") //请求路由
public class UserController {
    @Autowired
    IUserService userService;


    //增
    @PostMapping
    public ResponseMessage add(@Validated  @RequestBody UserDto user){
        User newUser =  userService.add(user);
        return ResponseMessage.success(newUser);
    }


    //查
    @GetMapping("/{userId}")
    public ResponseMessage get(@PathVariable Integer userId){
        User selectUser = userService.getUser(userId);
        return ResponseMessage.success(selectUser);
    }

    //改
    @PutMapping
    ResponseMessage edit(@Validated  @RequestBody UserDto user){
        User editUser =  userService.edit(user);
        return ResponseMessage.success(editUser);
    }

    //删
    @DeleteMapping("/{userId}")
    public ResponseMessage delete(@PathVariable Integer userId){
        userService.delete(userId);
        return ResponseMessage.success();
    }

    //登陆
    @PostMapping("/login")
    public ResponseMessage login(@RequestBody LoginUserDto user){
        //登陆操作
        return userService.login(user);
    }

    //登陆
    @RequestMapping ("/logout")
    public ResponseMessage logout(){
        //退出操作
        return userService.logout();
    }


}
